import urllib.request
import urllib.parse

# urlencode应用场景：多个参数的时候
# url = 'https://www.google.com.hk/search?q=周杰伦&sex=男'

# data = {
#     'q': '周杰伦',
#     'sex': '男',
# }
#
# a = urllib.parse.urlencode(data)
# print(a)

base_url = 'https://www.google.com.hk/search?'
data = {
    'q': '周杰伦',
    'sex': '男',
}

url = base_url + urllib.parse.urlencode(data)
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"
}

# 请求对象的定制
request = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request)
html = response.read().decode('utf-8')
print(html)
